package cn.alpha.intell.rbtlib.view.sysdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import cn.alpha.intell.rbtlib.R;

/**
 * @author : jinlongfeng
 * @version : V2.1.2
 * @description: 系统 loading Dialog
 * @time : 2016/12/12-10:48
 */

public class LoadingDialog extends Dialog {


    public boolean isCancelable() {
        return isCancelable;
    }

    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }

    private boolean isCancelable;
    private ImageView mImageViewLoading;
    AnimationDrawable mAnimationDrawable;

    public LoadingDialog(Context context) {
        this(context, R.style.dialog_loading);

    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        LayoutInflater layoutInflater  = getLayoutInflater();
        View rootView = layoutInflater.inflate(R.layout.dialog_loading,null);
        mImageViewLoading = (ImageView) rootView.findViewById(R.id.iv_loading);
        setContentView(rootView);

        //获取到当前Activity的Window
        Window dialog_window = getWindow();
        //获取到LayoutParams
        WindowManager.LayoutParams dialog_window_attributes = dialog_window.getAttributes();
        //设置宽度
        dialog_window_attributes.width=WindowManager.LayoutParams.MATCH_PARENT;
        //设置高度
        dialog_window_attributes.height=WindowManager.LayoutParams.MATCH_PARENT;
        dialog_window.setAttributes(dialog_window_attributes);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            mImageViewLoading.setImageResource(R.drawable.anim_loading);
            mAnimationDrawable = (AnimationDrawable) mImageViewLoading.getDrawable();
            mAnimationDrawable.start();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mAnimationDrawable != null) {
            mAnimationDrawable.stop();
        }
    }


    @Override
    public void onBackPressed() {
        if(isCancelable){
        super.onBackPressed();
        }
    }
}
